package com.cloudpark.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudpark.common.util.ObjectUtil;
import com.cloudpark.common.util.Pagination;
import com.cloudpark.model.entity.Estate;
import com.cloudpark.model.entity.Park;
import com.cloudpark.model.mapper.EstateMapper;
import com.cloudpark.model.mapper.ParkMapper;
import com.cloudpark.model.vo.ParkVo;
import com.cloudpark.service.ParkService;

@Service
public class ParkServiceImpl implements ParkService {

	@Autowired
	private ParkMapper parkMapper;
	@Autowired
	private EstateMapper estateMapper;
	
	@Override
	public Park queryByParkCode(String parkCode) {
		List<Park> list = parkMapper.selectByParkCode(parkCode);
		if (ObjectUtil.isNotBlank(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Pagination queryParkList(Map<String, String> params, int start,
			int length, Long estateId) {
		Pagination pagination = new Pagination();
        String city = null == params.get("city") ? "" : params.get("city").toString();
        String area = null == params.get("area") ? "" : params.get("area").toString();
        String parkName = null == params.get("parkName") ? "" : params.get("parkName").toString();
        Park paramVo = new Park();
        if (ObjectUtil.isNotBlank(city)) {
            paramVo.setCity(city);
        }
        if (ObjectUtil.isNotBlank(area)) {
            paramVo.setArea(area);
        }
        if (ObjectUtil.isNotBlank(parkName)) {
            paramVo.setName(parkName);
        }
        paramVo.setEstateId(estateId);
        int totalRows = parkMapper.countBySelective(paramVo);
        paramVo.setCurrentPage(start / length + 1);
        paramVo.setNumPerPage(length);
        paramVo.setTotalRows(totalRows);
        paramVo.initPageParams();
        pagination.setTotalRows(totalRows);
        List<Park> resultList = parkMapper.selectBySelective(paramVo);
        List<ParkVo> list = new ArrayList<ParkVo>();
        if (null != resultList && resultList.size()>=1) {
        	for (Park park : resultList) {
        		ParkVo parkVo = new ParkVo();
        		parkVo.copy(park);
        		Estate estate = estateMapper.selectByPrimaryKey(park.getEstateId());
        		if (null != estate && null != estate.getName()) {
        			parkVo.setEstateName(estate.getName());
        		}
        		list.add(parkVo);
        	}
        }
        pagination.setResultList(list);
        return pagination;
	}

	@Override
	public Park queryParkById(Long id) {
		return parkMapper.selectByPrimaryKey(id);
	}

	@Override
	public void saveOrUpdatePark(Park park) {
		if (null != park.getId() && park.getId() > 0) {
			parkMapper.updateByPrimaryKey(park);
		} else {
			parkMapper.insertSelective(park);
		}
	}

	@Override
	public void deleteParkById(Long id) {
		parkMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Park> queryParkList(Park park) {
		return parkMapper.selectBySelective(park);
	}

	@Override
	public List<Park> queryParkByEstateId(Long estateId) {
		return parkMapper.selectByEstateId(estateId);
	}

	@Override
	public String queryParkOptions(Long estateId) {
		List<Park> list = parkMapper.selectByEstateId(estateId);
		if (ObjectUtil.isNotBlank(list)) {
			StringBuilder sb = new StringBuilder();
			sb.append("<option value=\"\">-请选择-</option>");
			for (Park park : list) {
				sb.append("<option value=\"").append(park.getParkCode()).append("\">").append(park.getName()).append("</option>");
			}
			return sb.toString();
		}
		return null;
	}
}
